package com.yanqu.road.server.manager.activity.climbtower;

import com.yanqu.road.entity.activity.climbtower.ClimbTowerTopData;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.bussiness.player.ClimbTowerActivityBussiness;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.math.BigInteger;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description
 * @Author hyx
 * @Data 2022/09/07 10:00
 */
public class CrossClimbTowerTop {

    private static Logger logger = LogManager.getLogger(CrossClimbTowerTop.class.getName());

    private Map<Integer, ClimbTowerTopData> cellTopDataMap = new ConcurrentHashMap<>();
    private Map<Long, ClimbTowerTopData> userTopDataMap = new ConcurrentHashMap<>();
    private int activityId;
    private boolean needSave = false;
    private long endShowTime;

    public CrossClimbTowerTop(int activityId, long endShowTime) {
        this.activityId = activityId;
        this.endShowTime = endShowTime;
    }

    public long getEndShowTime() {
        return endShowTime;
    }

    public void setEndShowTime(long endShowTime) {
        this.endShowTime = endShowTime;
    }

    public void initTopData(ClimbTowerTopData topData) {
        if (null == topData) {
            return;
        }
        cellTopDataMap.put(topData.getCellNum(), topData);
        userTopDataMap.put(topData.getUserId(), topData);
    }

    public void save() {
        if (!needSave) {
            return;
        }
        needSave = false;
        for (ClimbTowerTopData topData : cellTopDataMap.values()) {
            if (null == topData) {
                continue;
            }
            if (topData.isInsertOption()) {
                ClimbTowerActivityBussiness.addClimbTowerTopData(topData);
            } else if (topData.isUpdateOption()) {
                ClimbTowerActivityBussiness.updateClimbTowerTopData(topData);
            }
        }
    }

    public ClimbTowerTopData getTopDataByCellNum(int cellNum) {
        return cellTopDataMap.get(cellNum);
    }

    public ClimbTowerTopData getTopDataByUserId(long userId) {
        return userTopDataMap.get(userId);
    }

    public void winUpdateTopData(long serverId, long userId, BigInteger ability, int pic, int lv,
                                 int oldCellNum, int nextCellNum, UserBaseInfo userBaseInfo) {
        ClimbTowerTopData nextTopData = cellTopDataMap.get(nextCellNum);
        ClimbTowerTopData oldTopData = cellTopDataMap.get(oldCellNum);
        if (null == nextTopData) {
            if (null != oldTopData) {
                oldTopData.setUserId(0);//userId为0表示无玩家占位
            }
            nextTopData = new ClimbTowerTopData();
            nextTopData.setActivityId(activityId);
            nextTopData.setCellNum(nextCellNum);
            nextTopData.setInsertOption();
            cellTopDataMap.put(nextCellNum, nextTopData);
        } else {
            if (null != oldTopData) {
                oldTopData.setUserId(nextTopData.getUserId());
                oldTopData.setServerId(nextTopData.getServerId());
                oldTopData.setPic(nextTopData.getPic());
                oldTopData.setLv(nextTopData.getLv());
                oldTopData.setAbility(nextTopData.getAbility());
                oldTopData.setUserBaseInfo(nextTopData.getUserBaseInfo());
                if (oldTopData.getUserId() > 0) {
                    userTopDataMap.put(oldTopData.getUserId(), oldTopData);
                }
            } else if (nextTopData.getUserId() > 0) {//玩家被打下榜
                userTopDataMap.remove(nextTopData.getUserId());
            }
        }

        nextTopData.setUserId(userId);
        nextTopData.setServerId(serverId);
        nextTopData.setPic(pic);
        nextTopData.setLv(lv);
        nextTopData.setAbility(ability);
        nextTopData.setUserBaseInfo(userBaseInfo);
        userTopDataMap.put(nextTopData.getUserId(), nextTopData);

        needSave = true;
    }
}
